<template>
	<view style="padding:10px">
		<u--form labelPosition="left" :model="userInfo" :rules="rules" ref="form1" labelWidth="50">
			<u-form-item label="头像" prop="avatar" borderBottom>
				<u-upload :fileList="fileList" @afterRead="afterRead" imageMode="scaleToFill" name="1" :maxCount="1"
					previewImage class="uimg">
					<!-- <u-avatar :src="uploadFilePath+userInfo.avatar"></u-avatar> -->
					<view v-if="userInfo.avatar">
						<image :src="uploadFilePath+userInfo.avatar" class="dplogo"/>
					</view>
					<view v-else>
						<image :src="userlogo" class="dplogo"/>
					</view>
				</u-upload>
			</u-form-item>
			<u-form-item label="姓名" prop="nickName" borderBottom >
				<u--input v-model="userInfo.nickName" border="none" placeholder="请输入姓名"></u--input>
			</u-form-item>
			<u-form-item label="电话" prop="userName" borderBottom >
				<u--input v-model="userInfo.userName" border="none" placeholder="请输入电话"></u--input>
			</u-form-item>
			<u-form-item label="性别" prop="sex" borderBottom >
				<u-radio-group
				  v-model="userInfo.sex"
				  placement="row"
				  @change="groupChange"
				>
				  <u-radio
				    :customStyle="{marginLeft: '8px'}"
				    v-for="(item, index) in radiolist1"
								  activeColor="#1E8550"
				    :key="index"
				    :label="item.name"
				    :name="item.name"
				    @change="radioChange"
				  >
				  </u-radio>
				</u-radio-group>
			</u-form-item>
		</u--form>
		<view style="width:96%;position:fixed;bottom:20rpx;left:50%;right:50%; transform: translate(-50%, -50%);" @click="submitForm">
		    <u-button  color="#1E8550" round size="large" >确定</u-button>
		</view>
	</view>
</template>

<script>
	import Config from '@/util/index.js'
	import {
		userEditInfo,getUserInfo,editUserlogo
	} from '@/api/user'
	import cookie from '@/utils/cookie'
	export default {
		data() {
			return {
				userInfo:{},
				srcImg:'',
				fileList:[],
				radiovalue1:'',
				userlogo:'',
				uploadFilePath:'',
				rules:{
					'dname': {
						type: 'string',
						required: true,
						message: '请填写商品名称',
						trigger: ['blur', 'change']
					},
					'dprice': {
						type: 'number',
						required: true,
						message: '请填写商品价格',
						trigger: ['blur', 'change']
					}
				},
				radiolist1: [{
					name: '男',
					disabled: false
				},
				{
					name: '女',
					disabled: false
				}]
			}
		},
		onLoad(){
			// let _this = this
			// uni.getStorage({
			// 	key: 'userifo',
			// 	success: function (res) {
			// 		_this.radiovalue1 = JSON.parse(res.data).sex=='1'?'男':'女'
			// 		_this.userInfo = JSON.parse(res.data)
			// 		console.log("this.radiovalue1",_this.radiovalue1)
			// 	}
			// })
			this.getUser()
		},
		created(){
			this.uploadFilePath = Config.get('apiUrl')
			this.userlogo = this.picUrl + "userlogo.png"
		},
		methods: {
			afterRead(event) {
				let file = event.file;
				let _this = this
				 const token = cookie.get('accessToken')
				uni.uploadFile({
					url: _this.uploadFilePath + '/common/upload',
					filePath: file.url,
					name: 'file',
					success(res) {
						let logoImg = JSON.parse(res.data)
						_this.userlogo = _this.imgpath + logoImg
						_this.userInfo.avatar = logoImg.fileName
						_this.fileList = []
						_this.fileList.push({
							...file,
							url: logoImg.url
						});
						console.log("item",_this.userInfo)
					}
				});
			},
			submitForm(){
				console.log("11221",this.userInfo)
				userEditInfo(this.userInfo).catch(resx => {
					this.getUser()
				})
			},
			getUser(){
				getUserInfo().catch(response=>{
				   if(response.data.code==200){
					   console.log("response.data",response.data)
					   this.radiovalue1 = response.data.data.sex=='1'?'男':'女'
					   this.userInfo = response.data.data
				   }
				})
			},
			groupChange(ev){
				console.log("222",ev)
			},
			radioChange(e){
				console.log("111",e)
			},
		}
	}
</script>

<style>
 .dplogo{width:60upx;height:60upx}
</style>
